<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const list = [{
      "id": "604e2251b205b95968e1312c",
      "pid": "",
      "name": "财务部",
      "code": "CWB",
      "manager": "管理员",
      "introduce": "财务部",
      "createTime": "2021-03-14 22:47:25"
    }, {
      "id": "604e2262b205b95968e1312d",
      "pid": "604e2251b205b95968e1312c",
      "name": "财务核算部",
      "code": "CWHSB",
      "manager": "管理员",
      "introduce": "财务核算部",
      "createTime": "2021-03-14 22:47:25"
    }, {
      "id": "604e227db205b95968e1312e",
      "pid": "604e2251b205b95968e1312c",
      "name": "税务管理部",
      "code": "SWGLN",
      "manager": "管理员",
      "introduce": "税务管理部",
      "createTime": "2021-03-14 22:47:25"
    }, {
      "id": "6051ad90e93db6522c1d00d2",
      "pid": "",
      "name": "技术部",
      "code": "JSBaaaaa",
      "manager": "巴思慧",
      "introduce": "技术部",
      "createTime": "2021-03-17 15:18:23"
    }, {
      "id": "6051adb6e93db6522c1d00d3",
      "pid": "6051ad90e93db6522c1d00d2",
      "name": "Java研发部",
      "code": "JYFB",
      "manager": "管理员",
      "introduce": "JAVA研发部",
      "createTime": "2021-03-17 15:18:23"
    }, {
      "id": "6051add6e93db6522c1d00d4",
      "pid": "6051ad90e93db6522c1d00d2",
      "name": "Python研发部",
      "code": "PYFB",
      "manager": "罗晓晓",
      "introduce": "Python研发部",
      "createTime": "2021-03-17 15:18:23"
    }]

   function fn(list, a){
     const arr =[]
     list.forEach(item => {
       if(item.pid===a){
        const b = fn(list,item.id)
        item.b=b
        arr.push(item)
       }
     })
     return arr
   }
  const cc= fn(list,'')
   console.log(cc)
  </script>
</body>

</html>